//
// Created by Kqj on 25-9-1.
//

#include "Tenser.h"
#include "stm32f1xx_hal.h"
#include "max6675.h"
#include "show.h"

extern float t;
extern uint16_t adc_value1;
extern uint16_t adc_value2;
extern uint16_t adc1_V;
extern uint16_t adc2_V;
extern int16_t temp;
extern ADC_HandleTypeDef hadc1;

extern ADC_HandleTypeDef hadc2;

uint16_t dong_get_adc1(){
    //开启ADC1
    HAL_ADC_Start(&hadc1);
    //等待ADC转换完成，超时为100ms
    HAL_ADC_PollForConversion(&hadc1,100);
    //判断ADC是否转换成功
    if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1),HAL_ADC_STATE_REG_EOC)){
        //读取值
        return HAL_ADC_GetValue(&hadc1);
    }
    return 0;
}

uint16_t dong_get_adc2(){
    //开启ADC1
    HAL_ADC_Start(&hadc2);
    //等待ADC转换完成，超时为100ms
    HAL_ADC_PollForConversion(&hadc2,100);
    //判断ADC是否转换成功
    if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc2),HAL_ADC_STATE_REG_EOC)){
        //读取值
        return HAL_ADC_GetValue(&hadc2);
    }
    return 0;
}

void Fresh_Tenser_data()
{
    //采集数据
    t = max6675_readTemperature();
    temp = (int16_t) t/4;

    adc_value1 = dong_get_adc1();
    adc1_V = (adc_value1-580.0)/4095.0*3.3*30;
    adc1_V = 0;
    adc_value2 = dong_get_adc2();
    adc2_V = adc_value2/4095.0*3.3*10*5.7; //电压*36.25psi/V

    show_down_info(temp,adc1_V,adc2_V);

}
